window.requestAnimFrame = (function () { //定义requestAnimFrame为window的方法 
  return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function (callback) {
      window.setTimeout(callback, 1000 / 60);
    };
})();
var totalDeg = 360 * 3 + 0;
var steps = [];
var prizeDeg = [158, 22, 67, 112, 202, 248, 293, 338];
// 0:158再接再厉
// 1:22 100M流量
// 2:67 10元电子券
// 3:112 20元电子券
// 4:202 50元电子券
// 5:248 100元电子券
// 6:293 1000元话费 
// 7:338 腾讯王卡
var now = 0;
var a = 0.12; //控制转的速度
var timer, running = false;
var i = 10;

function rotate(outter, prizetype) {
  //beforeSend
  running = true;
  timer = setInterval(fun1 = function () {
    i += 10;
    outter.style.webkitTransform = 'rotate(' + (-i) + 'deg)';
    outter.style.MozTransform = 'rotate(' + (-i) + 'deg)';
  }, 1);

  switch (Number(prizetype)) {
    case 0:
      start(prizeDeg[0]);
      break;
    case 1:
      start(prizeDeg[1]);
      break;
    case 2:
      start(prizeDeg[2]);
      break;
    case 3:
      start(prizeDeg[3]);
      break;
    case 4:
      start(prizeDeg[4]);
      break;
    case 5:
      start(prizeDeg[5]);
      break;
    case 6:
      start(prizeDeg[6]);
      break;
    case 7:
      start(prizeDeg[7]);
      break;
    default:
      start();
  }
  running = false;
};

function start(deg) {
  clearInterval(timer);
  totalDeg = 360 * 4 + deg; //设置转的圈数
  steps = [];
  now = 0;
  countSteps();
  requestAnimFrame(step);
}

function countSteps() {
  var t = Math.sqrt(2 * totalDeg / a);
  var v = a * t;
  for (var i = 0; i < t; i++) {
    steps.push((2 * v * i - a * i * i) / 2);
  }
  steps.push(totalDeg);
}

function step() {
  outter.style.webkitTransform = 'rotate(' + (-steps[now++]) + 'deg)';
  outter.style.MozTransform = 'rotate(' + (-steps[now++]) + 'deg)';
  if (now < steps.length) {
    requestAnimFrame(step); //递归调用
  } else {
    //递归结束条件
    running = false;
    setTimeout(function () {

    }, 200);
  }
}

function showResult() {
  switch (Number(prizetype)) {
    case 0:
      vm.prizename = "";
      vm.again = true;
      break;
    case 1:
      vm.prizename = "vivo NEX优惠券";
      vm.ticket = true;
      vm.movehref = "http://t.cn/RdQbhhp";
      break;
    case 2:
      vm.prizename = "华为P20优惠券";
      vm.ticket = true;
      vm.movehref = "http://t.cn/RdQbUgX";
      break;
    case 3:
      vm.prizename = "OPPO Find X优惠券";
      vm.ticket = true;
      vm.movehref = "http://t.cn/RdQbVVs";
      break;
    case 4:
      vm.prizename = "阿巴町V328儿童手表优惠券";
      vm.ticket = true;
      vm.movehref = "http://t.cn/RdQbawY";
      break;
    case 5:
      vm.prizename = "科大讯飞智能护眼台灯优惠券";
      vm.ticket = true;
      vm.movehref = "http://t.cn/RdQbpLU";
      break;
    case 6:
      vm.prizename = "小度智能音箱奖品优惠券";
      vm.ticket = true;
      vm.movehref = " http://t.cn/RdQbHiU";
      break;
    case 7:
      vm.prizename = "腾讯王卡";
      vm.mobject = true;
      break;
  };
  vm.times = vm.times - 1;
  vm.cantbtn = false;
}

module.exports = {
  rotate: rotate
}
